﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using TallyBook.BLL.Manger.Expend;
using TallyBook.BLL.Service;
using TallyBook.BLL.Service.Imp;

namespace TallyBook.UI
{
    public partial class IncomeAddForm : Form
    {
        private readonly IIncomeService<Income> _incomeService;
        private readonly List<TextBox> _textBoxs = new List<TextBox>();

        public IncomeAddForm(IIncomeService<Income> incomeService)
        {
            _incomeService = incomeService;
            InitializeComponent();
        }

        public Action RefreshMainForm { get; set; }

        private void IncomeAddForm_Load(object sender, EventArgs e)
        {
            _textBoxs.Add(txtId);
            _textBoxs.Add(txtMoney);
            _textBoxs.Add(txtComment);
            Member();
        }

        public void Member()
        {
            var account = new AccountService();
            cbbAccountType.DataSource = null;
            List<AccountType> getaccount = account.GetName();
            cbbAccountType.DataSource = getaccount;
            cbbAccountType.DisplayMember = "Name";
            cbbAccountType.ValueMember = "Id";

            var income = new IncomeService();
            cbbIncomeType.DataSource = null;
            List<IncomeType> getincome = income.GetName();
            cbbIncomeType.DataSource = getincome;
            cbbIncomeType.DisplayMember = "Name";
            cbbIncomeType.ValueMember = "Id";
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {

            if (_textBoxs.Any(item => string.IsNullOrEmpty(item.Text)))
            {
                MessageBox.Show(@"信息未填完整");
                return;
            }

            var detection = new Detection();
            var list = _textBoxs.Where(item => item.Name != "txtComment").ToArray();
            if (list.Any(detection.DetectionMoney))
            {
                MessageBox.Show(@"含非法字符");
                return;
            }



            long cbbincometypeid = long.Parse(cbbIncomeType.SelectedValue.ToString());
            long cbbaccounttypeid = long.Parse(cbbAccountType.SelectedValue.ToString());
            long id = long.Parse(txtId.Text);
            decimal money = decimal.Parse(txtMoney.Text);
            string comment = txtComment.Text;
            DateTime time = DateTime.Now;

            if (_incomeService.Insert(cbbincometypeid, cbbaccounttypeid, id, money, comment, time) != 0)
            {
                MessageBox.Show(@"添加成功");
                RefreshMainForm();
                Hide();
            }
           

            //switch (Detection())
            //{
            //    case 1:
            //        MessageBox.Show(@"账户填写错误");
            //        return;
            //    case 2:
            //        MessageBox.Show(@"名字过长");
            //        return;
            //    case 3:
            //        MessageBox.Show(@"金额出现非法字符");
            //        return;
            //}
        }

        /// <summary>
        ///     检测
        /// </summary>
        /// <returns></returns>
        private int Detection()
        {
            if (_textBoxs[0].Text.Any(item => item > 57 || item < 48))
                return 1;
            if (_textBoxs[1].Text.Length > 5)
                return 2;
            if (_textBoxs[2].Text.Any(item => item > 57 || item < 48))
                return 3;
            return _textBoxs[2].Text.Any(item => item > 57 || item < 48) ? 4 : 0;
        }
    }
}